<template>
  <div class="drug-list-container">
    <!-- 页面标题 -->
    <div class="page-header">
      <h3>货位 {{ huoweiId }} 下的商品变更记录</h3>
      <el-button type="primary" size="small" @click="$emit('back-to-huowei')">
        <i class="el-icon-back"></i> 返回货位列表
      </el-button>
    </div>

    <!-- 查询表单 -->
    <el-form :model="queryForm" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="drugName">
        <el-input
          v-model="queryForm.drugName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批号" prop="drugBatchNo">
        <el-input
          v-model="queryForm.drugBatchNo"
          placeholder="请输入批号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 商品汇总表格 -->
    <el-table v-loading="loading" :data="drugList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="药品ID" align="center" prop="drugId" />
      <el-table-column label="药品名称" align="center" prop="drugName" min-width="150" />
      <el-table-column label="批号" align="center" prop="drugBatchNo" />
      <el-table-column label="变更记录数" align="center" prop="changeCount">
        <template slot-scope="scope">
          <el-tag type="primary">{{ scope.row.changeCount }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="变更类型" align="center" prop="changeTypes" min-width="200">
        <template slot-scope="scope">
          <div class="change-types">
            <el-tag
              v-for="(type, index) in scope.row.changeTypes.split(',')"
              :key="index"
              :type="getChangeTypeTagType(type)"
              size="mini"
              class="type-tag"
            >
              {{ type }}
            </el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleViewDetails(scope.row)"
          >查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryForm.pageNum"
      :limit.sync="queryForm.pageSize"
      :page-sizes="[50, 100, 200, 500]"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getDrugSummary } from '@/api/report/inventoryChange'
import Pagination from '@/components/Pagination'
import RightToolbar from '@/components/RightToolbar'

export default {
  name: 'DrugList',
  components: {
    Pagination,
    RightToolbar
  },
  props: {
    huoweiId: {
      type: String,
      required: true
    },
    queryParams: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品汇总表格数据
      drugList: [],
      // 查询参数
      queryForm: {
        pageNum: 1,
        pageSize: 50,
        huoweiId: '',
        drugName: '',
        drugBatchNo: '',
        inventoryId: '',
        startTime: '',
        endTime: ''
      }
    }
  },
  created() {
    this.initQueryParams()
    this.getList()
  },
  methods: {
    /** 初始化查询参数 */
    initQueryParams() {
      this.queryForm.huoweiId = this.huoweiId
      if (this.queryParams.inventoryId) {
        this.queryForm.inventoryId = this.queryParams.inventoryId
      }
      if (this.queryParams.startTime) {
        this.queryForm.startTime = this.queryParams.startTime
      }
      if (this.queryParams.endTime) {
        this.queryForm.endTime = this.queryParams.endTime
      }
    },

    /** 查询商品汇总列表 */
    getList() {
      this.loading = true
      getDrugSummary(this.queryForm).then(response => {
        this.drugList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryForm.pageNum = 1
      this.getList()
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.queryForm.resetFields()
      this.queryForm.huoweiId = this.huoweiId // 保持货位ID
      this.handleQuery()
    },

    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.drugId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /** 导出按钮操作 */
    handleExport() {
      this.$download.excel(this.queryForm, `商品变更汇总_${new Date().getTime()}.xlsx`)
    },

    /** 查看变更详情 */
    handleViewDetails(row) {
      this.$emit('view-details', {
        drugId: row.drugId,
        drugBatchNo: row.drugBatchNo
      })
    },

    /** 获取变更类型标签类型 */
    getChangeTypeTagType(type) {
      const typeMap = {
        '正常': 'success',
        '数量更改': 'warning',
        '货位调换': 'info',
        '效期更改': 'danger',
        '批号更改': 'primary',
        '其它信息': ''
      }
      return typeMap[type] || ''
    }
  }
}
</script>

<style scoped>
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding: 10px 0;
  border-bottom: 1px solid #e4e7ed;
}

.page-header h3 {
  margin: 0;
  color: #303133;
}

.change-types {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  justify-content: center;
}

.type-tag {
  margin: 2px;
}
</style>
